﻿using Accord.IO;
using AForge.Imaging.Filters;
using AutoStar.Common;
using SharpGL.SceneGraph;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;

namespace AutoStar.Common
{
    public class AnalysisGCode
    {
        public static int height = 0;
        public static int layer = 0;
        public static List<PrintLayer> ParseGCode(string filePath)
        {
            List<PrintLayer> layers = new List<PrintLayer>();
            PrintLayer currentLayer = new PrintLayer();
            Point3D currentPoint = null;
            Regex regex = new Regex(@"^(G0|G1)\s+X([\d.]+)\s+Y([\d.]+)");

            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    try
                    {
                        line = line.Trim();
                        if (line.StartsWith(";"))
                        {
                            if (line.Contains(";Layer height:"))
                                height = ParseCommentLine(line);
                            else if (line.Contains(";LAYER:") || line.Contains("; total layer number:"))
                            {
                                layer = ParseCommentLine(line);
                                if (currentLayer != null)
                                    layers.Add(currentLayer);
                                currentLayer = new PrintLayer();
                                currentLayer.height = height * layer;
                                currentLayer.channel = 3;
                                currentLayer.mode = 2;
                                currentLayer.x_size = 20;
                                currentLayer.y_size = 20;
                                currentLayer.x_start = Global.printProject.printChannels[currentLayer.channel].position.X;
                                currentLayer.y_start = Global.printProject.printChannels[currentLayer.channel].position.X;
                                currentLayer.z_start = 0;
                                currentLayer.solidfyTime = 0;
                            }
                        }
                        //else if (line.StartsWith("G0") || line.StartsWith("G1"))
                        else if (line.StartsWith("G1"))
                        {
                            Match match = regex.Match(line);
                            if (match.Success)
                            {
                                currentPoint = ParseGCodeLine(line);
                                if (currentPoint != null)
                                {
                                    currentLayer.points.Add(currentPoint);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
                if (currentLayer != null)
                {
                    layers.Add(currentLayer);
                }
            }
            layers.RemoveAt(0);
            return layers;
        }
        private static int ParseCommentLine(string line)
        {
            try
            {
                string[] parts = line.Split(':');
                if (parts.Length == 2)
                {
                    string parameterName = parts[0].TrimStart(';').Trim();
                    int parameterValue = int.Parse(parts[1].Trim());
                    return parameterValue;
                }
                return 0;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        private static int currentMode = 1;
        private static Point3D ParseGCodeLine(string line)
        {
            try
            {
                Point3D point = new Point3D();
                string[] parts = line.Split(' ');
                foreach (string part in parts)
                {
                    if (part.StartsWith("X"))
                    {
                        point.X = float.Parse(part.Substring(1));
                    }
                    else if (part.StartsWith("Y"))
                    {
                        point.Y = float.Parse(part.Substring(1));
                    }
                    else if (part.StartsWith("Z"))
                    {
                        point.Z = float.Parse(part.Substring(1));
                    }
                    //point.mode = line.StartsWith("G1") ? 1 : 0;
                }
                point.mode = currentMode; 
                //currentMode = 1 - currentMode; // 切换模式
                return point;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
